package control
{
	import com.greensock.TweenLite;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	
	import loader.SinglePicLoader;
	
	import utils.MyTimer;
	import utils.NumberTool;
	
	public class FullScreenAutoPicShow_0 extends EventDispatcher
	{
		protected var bmps:Vector.<Bitmap>;
		protected var curNum:uint=0;
		protected var max_width:Number;
		protected var max_height:Number;
		protected var waitTime:Number;
		protected var durationTime:Number;
		protected var picUrls:Vector.<String>;
		protected var con:Sprite;
		protected var max_off_x:uint;//整数 最大偏移
		protected var max_off_y:uint;
		protected var max_scale:uint;//整数 最大缩放尺寸 1.X  1.Y
		protected var startDirects:Vector.<uint>=new <uint>[0,1,2,3,4,5,6,7];//上下左右 左上,左下,右上,右下 八个方向
		protected var singlePicLoader:SinglePicLoader;
		protected var playTimer:MyTimer;
		protected var directNum:uint=0;
		protected var close:Boolean=false;
		public function FullScreenAutoPicShow_0(_con:Sprite,pic_urls:Vector.<String>,_du:Number,wait:Number,w:Number,h:Number,m_x:uint,m_y:uint,s_x:uint,target:IEventDispatcher=null)
		{
			super(target);
			con=_con;
			picUrls=pic_urls;
			durationTime=_du;
			waitTime=wait;
			max_width=w;
			max_height=h;
			max_off_x=m_x;
			max_off_y=m_y;
			max_scale=s_x;
			//con必须先加入stage
			showNext();
		}
		
		protected function showNext():void
		{
			if(close)return;
			if(!bmps)
			{
				bmps=new Vector.<Bitmap>();
			}
			singlePicLoader=new SinglePicLoader(picUrls[curNum],handleBitmapLoaded);
		}
		
		protected function addPlayTimer():void
		{
			if(close)return;
			if(!playTimer)
			{
				playTimer=new MyTimer(showNext,waitTime,1);
				playTimer.start();
			}
		}
		
		protected function removePlayTimer():void
		{
			if(playTimer)
			{
				playTimer.GC();
				playTimer=null;
			}
		}
		
		protected function handleBitmapLoaded(bm:Bitmap):void
		{
			singlePicLoader=null;
			removePlayTimer();
			if(close)return;
			con.addChild(bm);
			bmps.push(bm);
			var targetX:Number=max_width/2-bm.width/2;
			var targetY:Number=max_height/2-bm.height/2;
			
			
			var startDirect:uint=startDirects[getStartDirect()];
			var startX:Number=getStartX(startDirect);
			var startY:Number=getStartY(startDirect);
			var startScale:Number=getStartScale();
			trace("startDirect="+startDirect);
			bm.scaleX=bm.scaleY=startScale;
			bm.x=startX;
			bm.y=startY;
			bm.alpha=0;
			
			TweenLite.to(bm,durationTime,{alpha:1,x:targetX,y:targetY,scaleX:1,scaleY:1,onComplete:playNext});
		}
		
		protected function removeLastBm():void
		{
			if(bmps.length>1)
			{
				var bm:Bitmap=bmps[0] as Bitmap;
				
				if(con.contains(bm))
				{
					con.removeChild(bm);
				}
				bm.bitmapData.dispose();
				bmps.splice(0,1);
			}
		}
		
		protected function removeBmps():void
		{
			if(bmps)
			{
				var bm:Bitmap;
				var length:uint=bmps.length;
				for(var i:uint=0;i<length;i++)
				{
					bm=bmps[i] as Bitmap;
					bm.bitmapData.dispose();
				}
			}
		}
		
		protected function playNext():void
		{
			removeLastBm();
			if(curNum==picUrls.length-1)
			{
				curNum=0;
			}
			else
			{
				curNum++;
			}
			addPlayTimer();
		}
		
		protected function getStartScale():Number
		{
			var scale:uint=NumberTool.getRoundRandomNumber(0,max_scale);
			return Number("1."+scale);
		}
		
		
		protected function getStartX(s:uint):Number
		{
			if(s==0||s==1)
			{
				//当在上或者下的时候
				return 0;
			}
			else if(s==2||s==4||s==5)
			{
				//当在左边的时候
				return -(NumberTool.getRoundRandomNumber(0,max_off_x));
			}
			else
			{
				//当在右边的时候
				return NumberTool.getRoundRandomNumber(0,max_off_x);
			}
		}
		
		protected function getStartY(s:uint):Number
		{
			if(s==2||s==3)
			{
				//当在左或者右的时候
				return 0;
			}
			else if(s==0||s==4||s==6)
			{
				//当在上边的时候
				return -(NumberTool.getRoundRandomNumber(0,max_off_y));
			}
			else
			{
				//当在下边的时候
				return NumberTool.getRoundRandomNumber(0,max_off_y);
			}
		}
		
		protected function getStartDirect():uint
		{
			return NumberTool.getRoundRandomNumber(0,7);
			//return curNum;
			if(directNum==startDirects.length-1)
			{
				directNum=0;
			}
			else
			{
				directNum++;
			}
			return directNum;
		}
		
		public function gc():void
		{
			close=true;
			removePlayTimer();
			removeBmps();
		}
	}
}